package com.foxzzz.dynamic.programming;

public class Solution32 {
    public int longestValidParentheses(String s) {
        char[] array = s.toCharArray();
        int num = 0;
        int max = -1;
        for (int i = 0; i < array.length; i++) {
            num = 0;
            for (int j = i; j < array.length; j++) {
                int item = array[j] == '(' ? 1 : -1;
                num += item;
                if (num == 0) {
                    if (max < j - i) {
                        max = j - i;
                    }
                } else if (num < 0) {
                     break;
                }
            }
        }
        return max+1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution32().longestValidParentheses("(()"));
        System.out.println(new Solution32().longestValidParentheses(")()())"));
    }
}
